package com.atguigu.shiro.mapper;

import com.atguigu.shiro.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * className: UserMapper
 * description:
 * date: 2024/6/18-10:24
 * <p>
 * project: shiro
 * package: com.atguigu.shiro.mapper
 * email: 1085844536@qq.com
 * version:
 *
 * @author WangGuojian
 */
@Repository
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据用户名查询角色信息
     *
     * @param principal {@link String}
     * @return {@link List<String>}
     */
    @Select("SELECT NAME FROM role WHERE id IN (SELECT rid FROM role_user WHERE uid=(SELECT id FROM USER WHERE NAME =#{principal}))")
    List<String> getUserRoleInfoMapper(@Param("principal") String principal);

    /**
     * 根据角色查询权限信息
     *
     * @param roles {@link List<String>}
     * @return {@link List<String>}
     */
    @Select({
            "<script>",
            "select info FROM permissions WHERE id IN ",
            "(SELECT pid FROM role_ps WHERE rid IN (",
            "SELECT id FROM role WHERE NAME IN ",
            "<foreach collection='roles' item='name' open='(' separator=',' close=')'>",
            "#{name}",
            "</foreach>",
            "))",
            "</script>"
    })
    List<String> getUserPermissionInfoMapper(@Param("roles") List<String> roles);
}
